package com.gagakuai.engine.impl.pvm.process;

import com.gagakuai.engine.exception.ActivitiException;
import com.gagakuai.engine.impl.delegate.ActivityExecution;
import com.gagakuai.engine.impl.pvm.PvmProcessDefinition;
import com.gagakuai.engine.impl.pvm.PvmProcessInstance;
import com.gagakuai.engine.impl.runtime.ExecutionImpl;
import com.gagakuai.engine.impl.runtime.InterpretableExecution;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @program: common-starters
 * @description: 流程实现类
 * @author: houhong
 * @create: 2023-01-02 11:23
 */
public class ProcessDefinitionImpl extends ScopeImpl implements PvmProcessDefinition {

    protected String name;
    protected String key;
    protected String description;
    //流程定义的初始值
    protected ActivityImpl initial;

    protected Map<ActivityImpl, List<ActivityImpl>> initialActivityStacks = new HashMap<>();

    @Override
    public PvmProcessInstance createProcessInstance() {
        if (initial == null) {
            throw new ActivitiException("Process '" + name + "' has no default start activity (e.g. none start event), " +
                    "   hence you cannot use 'startProcessInstanceBy...' but have to start it using one of the modeled start events (e.g. message start events).");
        }
        return createProcessInstanceForInitial(initial);
    }


    public PvmProcessInstance createProcessInstanceForInitial(ActivityImpl initial) {

        if (initial == null) {
            throw new ActivitiException("Cannot start process instance, initial activity where the process instance should start is null.");
        }

        InterpretableExecution scopeInstance = newProcessInstance(initial);

        List<ActivityImpl> initialActivityStack = getInitialActivityStack(initial);
        for (ActivityImpl activity : initialActivityStack) {
            if (activity.isScope) {
                scopeInstance = (InterpretableExecution) scopeInstance.createExecution();
                scopeInstance.setActivity(activity);
                if (activity.isScope) {
                    //// TODO: 2023/1/2
                }
            }
        }
        scopeInstance.setActivity(initial);
        return scopeInstance;
    }

    public synchronized List<ActivityImpl> getInitialActivityStack(ActivityImpl startActivity) {
        List<ActivityImpl> initialActivityStack = initialActivityStacks.get(startActivity);
        if (initialActivityStack == null) {
            initialActivityStack = new ArrayList<ActivityImpl>();
            ActivityImpl activity = startActivity;
            while (activity != null) {
                initialActivityStack.add(0, activity);
                activity = activity.getParentActivity();
            }
            initialActivityStacks.put(startActivity, initialActivityStack);
        }
        return initialActivityStack;
    }

    protected InterpretableExecution newProcessInstance(ActivityImpl activity) {

        return new ExecutionImpl(activity);
    }

    @Override
    public ActivityImpl getInitial() {
        return initial;
    }

}